package com.fulihui.usercore.biz.conv;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.near.toolkit.common.StringUtil;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.fulihui.usercore.condition.MerchantCondition;
import com.fulihui.usercore.dal.dataobj.MerchantDO;
import com.fulihui.usercore.dto.MerchantDTO;
import com.fulihui.usercore.request.merchant.MerchantSaveRequest;

/**
 * @author Willard.Hu on 2016/9/25.
 */
public class MerchantConv {

    public static MerchantDO toDO(MerchantSaveRequest request, Map<String, Object> old) {
        if (request == null) {
            return null;
        }
        MerchantDO dataobj = new MerchantDO();
        dataobj.setMid(request.getMid());
        dataobj.setLogoUrl(request.getLogoUrl());
        dataobj.setName(request.getName());
        dataobj.setContactName(request.getContactName());
        dataobj.setContactMobile(request.getContactMobile());
        dataobj.setCompanyAddress(request.getCompanyAddress());
        dataobj.setContractImage(request.getContractImage());
        dataobj.setRoleCode(request.getRoleCode());
        dataobj.setState(request.getState());
        dataobj.setDesc(request.getDesc());
        if (request.getExtInfo() != null) {
            if (old == null) {
                old = new HashMap<>();
            }
            request.getExtInfo().forEach(old::put);
            dataobj.setExtInfo(JSONObject.toJSONString(old));
        }
        return dataobj;
    }

    public static MerchantDO toDO(MerchantCondition condition) {
        if (condition == null) {
            return null;
        }
        MerchantDO dataobj = new MerchantDO();
        dataobj.setName(condition.getName());
        dataobj.setState(condition.getState());
        return dataobj;
    }

    public static MerchantDTO toDTO(MerchantDO dataobj) {
        if (dataobj == null) {
            return null;
        }
        MerchantDTO dto = new MerchantDTO();
        dto.setMid(dataobj.getMid());
        dto.setLogoUrl(dataobj.getLogoUrl());
        dto.setName(dataobj.getName());
        dto.setContactName(dataobj.getContactName());
        dto.setContactMobile(dataobj.getContactMobile());
        dto.setCompanyAddress(dataobj.getCompanyAddress());
        dto.setContractImage(dataobj.getContractImage());
        dto.setRoleCode(dataobj.getRoleCode());
        dto.setState(dataobj.getState());
        dto.setDesc(dataobj.getDesc());
        if (StringUtil.isNotBlank(dataobj.getExtInfo())) {
            dto.setExtInfo(JSONObject.parseObject(dataobj.getExtInfo()));
        }
        dto.gmtCreate(dataobj.getGmtCreate());
        dto.gmtModified(dataobj.getGmtModified());
        return dto;
    }

    public static List<MerchantDTO> toList(List<MerchantDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return Collections.emptyList();
        }
        return doList.stream().map(MerchantConv::toDTO).collect(Collectors.toList());
    }
}
